package com.qf.jie.utils;

import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class DBUtils {

    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    static {
        try {
            ClassLoader loader = DBUtils.class.getClassLoader();
            InputStream is = loader.getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(is);

            driver = properties.getProperty("driver");
            Class.forName(driver);
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }


    public static Connection getConnection(){
        try {
            Connection conn = DriverManager.getConnection(url,username,password);
            return conn;
        } catch ( Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void closeAll(AutoCloseable ... closeables){
        for (AutoCloseable clo:closeables){
            if (clo != null){
                try {
                    clo.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 更新操作，返回int
    public static int excuteUpdate(String sql, Object ...objs){
        Connection connection = null;
        PreparedStatement ps = null;
        int num = 0;
        try {
            connection = getConnection();
            ps = connection.prepareStatement(sql);
            if (objs != null){
                for (int i=0; i<objs.length; i++){
                    ps.setObject(i+1, objs[i]);
                }
            }
            num = ps.executeUpdate();
        } catch ( Exception throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(ps, connection);
        }
        return num;
    }
    //查询，结果为int
    public static int excuteQuery(String sql, Object ... objs){
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        int num = 0;
        try {
            connection = getConnection();
            ps = connection.prepareStatement(sql);
            for (int i=0; i<objs.length; i++){
                ps.setObject(i+1, objs[i]);
            }
            resultSet = ps.executeQuery();
            if (resultSet.next()){
                num = resultSet.getInt(1);
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(ps, connection);
        }
        return num;
    }

    //查询，结果为 List
    public static List excuteQuery(Class clazz, String sql, Object ... objs){
        List list = new ArrayList();
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            ps = connection.prepareStatement(sql);
            if (objs != null){
                for (int i=0; i<objs.length; i++){
                    ps.setObject(i+1, objs[i]);
                }
            }
            resultSet = ps.executeQuery();
            while (resultSet.next()){
                Constructor constructor = clazz.getConstructor();
                Object obj = constructor.newInstance();
                Field[] fields = clazz.getDeclaredFields();
                for (Field field:fields){
                    field.setAccessible(true);
                    field.set(obj, resultSet.getObject(field.getName()));
                }
                list.add(obj);
            }
        } catch ( Exception throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtils.closeAll(resultSet, ps, connection);
        }
        return list;
    }
















}
